package com.banciyuan.bcywebview.biz.main.newdiscover;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.helper.push.PushDataHelper;
import com.banciyuan.bcywebview.base.helper.timelinedatacenter.DetailDataHelper;
import com.banciyuan.bcywebview.base.helper.timelinedatacenter.TimelineLikeHelper;
import com.banciyuan.bcywebview.biz.main.discover.topwork.TopWorkActivity;
import com.banciyuan.bcywebview.biz.pc.zone.smooth.SmoothPersonActivity;
import com.banciyuan.bcywebview.utils.constants.MyConstants;
import com.banciyuan.bcywebview.utils.http.VolleyQueue;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;
import com.banciyuan.bcywebview.utils.string.StringUtil;
import com.banciyuan.bcywebview.utils.universalimageloader.core.ImageLoader;
import com.banciyuan.bcywebview.utils.universalimageloader.utils.ImageOptions;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.daoexample.model.HotDiscover;
import de.greenrobot.daoexample.model.Timeline;

/**
 * Created by squall on 2016/5/26.
 */
public class DiscoverListAdapter extends BaseAdapter {
    private List<HotDiscover> hlist = new ArrayList<>();
    private Context mContext;
    private LayoutInflater inflater;
    private RequestQueue mQueue;
    private String type;

    private boolean isAnimationRunning;

    public DiscoverListAdapter(Context context, List<HotDiscover> hlist, String type) {
        this.mContext = context;
        this.hlist = hlist;
        this.type = type;
        this.inflater = LayoutInflater.from(context);
        this.mQueue = VolleyQueue.getRquest(context);
    }


    @Override
    public int getCount() {
        return hlist.size() % 2 == 0 ? hlist.size() / 2 : hlist.size() / 2 + 1;
    }

    @Override
    public Object getItem(int position) {
        return hlist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    private class ViewHolder {
        public ImageView[] covers;
        public TextView[] unames;
        public ImageView[] zan_ivs;
        public ImageView[] events;
        public TextView[] zan_counts;
        public LinearLayout[] labels;
        public LinearLayout[] container;
        public TextView[] rank_tv;
        public TextView[] rank_en_tv;


        public ViewHolder(View convertView) {
            covers = new ImageView[]{(ImageView) convertView.findViewById(R.id.cover_one),
                    (ImageView) convertView.findViewById(R.id.cover_two)};
            unames = new TextView[]{(TextView) convertView.findViewById(R.id.uname_one),
                    (TextView) convertView.findViewById(R.id.uname_two)};
            zan_ivs = new ImageView[]{(ImageView) convertView.findViewById(R.id.zan_label_one),
                    (ImageView) convertView.findViewById(R.id.zan_label_two)};
            zan_counts = new TextView[]{(TextView) convertView.findViewById(R.id.zan_count_one),
                    (TextView) convertView.findViewById(R.id.zan_count_two)};
            container = new LinearLayout[]{(LinearLayout) convertView.findViewById(R.id.container_one),
                    (LinearLayout) convertView.findViewById(R.id.container_two)};
            labels = new LinearLayout[]{(LinearLayout) convertView.findViewById(R.id.cover_label_one),
                    (LinearLayout) convertView.findViewById(R.id.cover_label_two)};
            rank_tv = new TextView[]{(TextView) convertView.findViewById(R.id.rank_tv_one),
                    (TextView) convertView.findViewById(R.id.rank_tv_two)};
            rank_en_tv = new TextView[]{(TextView) convertView.findViewById(R.id.rank_en_tv_one),
                    (TextView) convertView.findViewById(R.id.rank_en_tv_two)};
            events = new ImageView[]{(ImageView) convertView.findViewById(R.id.event_label_one),
                    (ImageView) convertView.findViewById(R.id.event_label_two)};
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.daily_recommend_item, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        int left = position * 2;
        int right = position * 2 + 1;

        HotDiscover leftItem = hlist.get(left);
        HotDiscover rightItem = null;
        if (right < hlist.size()) {
            viewHolder.container[1].setVisibility(View.VISIBLE);
            rightItem = hlist.get(right);
            renderData(viewHolder, rightItem, 1);
        } else {
            viewHolder.container[1].setVisibility(View.INVISIBLE);
        }
        renderData(viewHolder, leftItem, 0);
        return convertView;
    }


    private void renderData(ViewHolder viewHolder, final HotDiscover hotDiscover, int index) {
        OnClick onClick = new OnClick(hotDiscover);
        if (!TextUtils.isEmpty(hotDiscover.getImg_src())) {
            viewHolder.covers[index].setVisibility(View.VISIBLE);
            ImageLoader.getInstance().displayImage(hotDiscover.getImg_src(), viewHolder.covers[index], ImageOptions.options);
        } else {
            viewHolder.covers[index].setVisibility(View.INVISIBLE);
        }
        viewHolder.events[index].setVisibility(View.GONE);
        viewHolder.labels[index].setVisibility(View.GONE);
        if (TextUtils.isEmpty(hotDiscover.getType())) {
            viewHolder.zan_ivs[index].setVisibility(View.VISIBLE);
            if (!TextUtils.isEmpty(hotDiscover.getUname())) {
                if (type.equals("cos")) {
                    viewHolder.unames[index].setText(Html.fromHtml(String.format(mContext.getString(R.string.coser_discover_format), hotDiscover.getUname())));
                } else if (type.equals("illust")) {
                    viewHolder.unames[index].setText(Html.fromHtml(String.format(mContext.getString(R.string.illust_discover_format), hotDiscover.getUname())));
                }
            }
            if (hotDiscover.isHave_ding()) {
                viewHolder.zan_ivs[index].setImageResource(R.drawable.detail_praise_done);
            } else {
                viewHolder.zan_ivs[index].setImageResource(R.drawable.detail_praise);
            }
            if (hotDiscover.getDing_num() > 9999) {
                String num = hotDiscover.getDing_num() / 1000 + "k";
                viewHolder.zan_counts[index].setText(num);
            } else {
                if (hotDiscover.getDing_num() == 0) {
                    viewHolder.zan_counts[index].setText(mContext.getString(R.string.good));
                } else {
                    viewHolder.zan_counts[index].setText(String.format(mContext.getString(R.string.zan_unit), String.valueOf(hotDiscover.getDing_num())));
                }
            }
            viewHolder.unames[index].setOnClickListener(onClick);
        } else {
            if (!TextUtils.isEmpty(hotDiscover.getUname())) {
                viewHolder.unames[index].setText(hotDiscover.getUname());
            }
            if (StringUtil.notNullEqual(hotDiscover.getType(), "event")) {
                viewHolder.events[index].setVisibility(View.VISIBLE);
                viewHolder.zan_counts[index].setText(mContext.getString(R.string.check_event_detail));
                viewHolder.zan_counts[index].setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        PushDataHelper.processInnerPushData(mContext, hotDiscover.getCode());
                    }
                });
            } else if (StringUtil.notNullEqual(hotDiscover.getType(), "rank")) {
                viewHolder.labels[index].setVisibility(View.VISIBLE);
                if (!TextUtils.isEmpty(hotDiscover.getUname())) {
                    if (hotDiscover.getUname().contains(mContext.getString(R.string.cos))) {
                        if (hotDiscover.getUname().length() >= 6) {
                            viewHolder.rank_tv[index].setText(hotDiscover.getUname().substring(3, 6));
                        } else {
                            viewHolder.rank_tv[index].setText(hotDiscover.getUname());
                        }
                        viewHolder.rank_en_tv[index].setText(mContext.getString(R.string.cos_ranking));
                    } else if (hotDiscover.getUname().contains(mContext.getString(R.string.draw))) {
                        if (hotDiscover.getUname().length() >= 5) {
                            viewHolder.rank_tv[index].setText(hotDiscover.getUname().substring(2, 5));
                        } else {
                            viewHolder.rank_tv[index].setText(hotDiscover.getUname());
                        }
                        viewHolder.rank_en_tv[index].setText(mContext.getString(R.string.painting_ranking));
                    }
                }
                viewHolder.zan_counts[index].setText(mContext.getString(R.string.check_all_rank));
            }
            viewHolder.zan_ivs[index].setVisibility(View.GONE);
            viewHolder.unames[index].setOnClickListener(null);
        }
        viewHolder.zan_ivs[index].setOnClickListener(onClick);
        viewHolder.container[index].setOnClickListener(onClick);
    }

    private void doZan(final HotDiscover hotDiscover, final View view, final int posistion) {
        if (isAnimationRunning) return;
        final Timeline mItem = new Timeline();
        mItem.setOtype_data(MyConstants.OTYPE_GROUP_DATA);
        if (!TextUtils.isEmpty(hotDiscover.getDp_id())) {
            mItem.setOtype(MyConstants.OTYPE_TIMELINE_ILL_NAME);
            mItem.setDp_id(hotDiscover.getDp_id());
        } else if (!TextUtils.isEmpty(hotDiscover.getCp_id())) {
            mItem.setOtype(MyConstants.OTYPE_TIMELINE_COS_NAME);
            mItem.setCp_id(hotDiscover.getCp_id());
        }
        mItem.setRp_id(hotDiscover.getRp_id());
        mItem.setHaveDing(String.valueOf(hotDiscover.isHave_ding()));

        TimelineLikeHelper.doLikeAll(mContext, mQueue, mItem, new TimelineLikeHelper.TimelineSuccess() {
            @Override
            public void onTimelineSuccess() {
                if (!hotDiscover.isHave_ding()) {
                    hotDiscover.setHave_ding(true);
                    hotDiscover.setDing_num(hotDiscover.getDing_num() + 1);
                } else {
                    hotDiscover.setHave_ding(false);
                    hotDiscover.setDing_num(hotDiscover.getDing_num() - 1);
                }
                renderPaise(view, hotDiscover, posistion);
            }
        });
    }

    private void renderPaise(View view, final HotDiscover hotDiscover, int position) {
        final ImageView iv = (ImageView) view;
        int id = 0;
        if (position == 0) {
            id = R.id.zan_count_one;
        } else {
            id = R.id.zan_count_two;
        }
        final TextView tv = (TextView) ((View) (iv.getParent())).findViewById(id);
        final StringBuilder result = new StringBuilder();
        String text = "";
        if (hotDiscover.getDing_num() > 9999) {
            text = hotDiscover.getDing_num() / 1000 + "k";
        } else {
            if (hotDiscover.getDing_num() == 0) {
                text = "赞";
            } else {
                text = String.format(mContext.getString(R.string.zan_unit), String.valueOf(hotDiscover.getDing_num()));
            }
        }
        result.append(text);
        if (hotDiscover.isHave_ding()) {
            isAnimationRunning = true;
            iv.setScaleY(2f);
            iv.setScaleX(2f);
            iv.setImageResource(R.drawable.frame_praise);
            AnimationDrawable drawable = (AnimationDrawable) iv.getDrawable();
            drawable.start();
            iv.postDelayed(new Runnable() {
                @Override
                public void run() {
                    iv.setScaleY(1f);
                    iv.setScaleX(1f);
                    iv.setImageResource(R.drawable.detail_praise_done);
                    tv.setText(result.toString());
                    isAnimationRunning = false;
                }
            }, 24 * 20);
        } else {
            iv.setImageResource(R.drawable.detail_praise);
            tv.setText(result.toString());
        }
    }

    private class OnClick implements View.OnClickListener {

        private HotDiscover hotDiscover;

        public OnClick(HotDiscover hotDiscover) {
            this.hotDiscover = hotDiscover;
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.zan_label_one:
                    doZan(hotDiscover, v, 0);
                    break;
                case R.id.zan_label_two:
                    doZan(hotDiscover, v, 1);
                    break;
                case R.id.uname_two:
                case R.id.uname_one:
                    gotoUtil.gotoAct(mContext, SmoothPersonActivity.class, hotDiscover.getUid());
                    break;
                case R.id.container_one:
                case R.id.container_two:
                    if (TextUtils.isEmpty(hotDiscover.getType())) {
                        if (type.equals("illust")) {
                            DetailDataHelper.gotoDrawerDetail(mContext, hotDiscover.getRp_id(), hotDiscover.getDp_id(), false);
                        } else if (type.equals("cos")) {
                            DetailDataHelper.gotoCosDetail(mContext, hotDiscover.getRp_id(), hotDiscover.getCp_id(), false);
                        }
                    } else {
                        if (StringUtil.notNullEqual(hotDiscover.getType(), "event")) {
                            PushDataHelper.processInnerPushData(mContext, hotDiscover.getCode());
                        } else if (StringUtil.notNullEqual(hotDiscover.getType(), "rank")) {
                            if (type.equals("cos")) {
                                gotoUtil.gotoAct(mContext, TopWorkActivity.class, 0, hotDiscover.getRank_type());
                            } else if (type.equals("illust")) {
                                gotoUtil.gotoAct(mContext, TopWorkActivity.class, 1, hotDiscover.getRank_type());
                            }
                        }
                    }
                    break;
            }
        }
    }
}
